package com.v2future.sjt.collegecheckingattendance.rollcall;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.library.Constant;
import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.rollcall.adapter.ClassAdapter;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.ClassBean;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.SubjectBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by minnie on 16/1/21.
 * 选择班级
 */
public class SelectClassActivity extends MainActivity implements AdapterView.OnItemClickListener{

    ListView classesLv;

    ClassAdapter adapter;

    List<ClassBean> dataList;

    ClassBean currentSelect;

    public static final String INTENT_TAG_CURRENTCLASS = "current_class";

    @Override
    public void init() {
        currentSelect = (ClassBean) getIntent().getSerializableExtra(INTENT_TAG_CURRENTCLASS);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_select_class);
        initTitleBack(new onBackClick());
        setTitleText(getString(R.string.select_class));
        classesLv = (ListView) findViewById(R.id.lv_classes);
    }

    @Override
    public void initAfter() {
        doGetClass();
    }

    public void refreshView(){
        //添加选中项
        if (null != currentSelect) {
            for (int i=0;i<dataList.size();i++) {
                if (currentSelect.getClassCode().equals(dataList.get(i).getClassCode())) {
                    dataList.get(i).setChecked(true);
                }
            }
        }
        adapter = new ClassAdapter(this, dataList);
        classesLv.setAdapter(adapter);
        classesLv.setOnItemClickListener(this);
    }

    public void doGetClass(){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.CLASS));
        NetUtils.doGetRequest(this, params, new NetTaskListener<ClassResponseBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(ClassResponseBean result) {
                dataList = result.getData();
                refreshView();
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(ClassResponseBean result) {
                tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {

            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(ClassResponseBean result) {

            }
        });
    }

    /**
     * Callback method to be invoked when an item in this AdapterView has
     * been clicked.
     * <p/>
     * Implementers can call getItemAtPosition(position) if they need
     * to access the data associated with the selected item.
     *
     * @param parent   The AdapterView where the click happened.
     * @param view     The view within the AdapterView that was clicked (this
     *                 will be a view provided by the adapter)
     * @param position The position of the view in the adapter.
     * @param id       The row id of the item that was clicked.
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        for (int i=0;i<dataList.size();i++) {
            dataList.get(i).setChecked(false);
        }
        dataList.get(position).setChecked(true);
        currentSelect = dataList.get(position);
        adapter.notifyDataSetChanged();
    }

    /**
     * 返回键的按键事件
     */
    public class onBackClick implements View.OnClickListener {

        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SelectClassActivity.this, HomeActivity.class);
            intent.putExtra("id", HomeActivity.FLAG_ROLLCALL);//回到主页点名fragment
            intent.putExtra(INTENT_TAG_CURRENTCLASS, currentSelect);
            intent.setAction(INTENT_TAG_CURRENTCLASS);
            gobackWithData(intent);
        }
    }

    public class ClassResponseBean extends BaseResponseBean implements Serializable {

        List<ClassBean> data;

        public List<ClassBean> getData() {
            return data;
        }

        public void setData(List<ClassBean> data) {
            this.data = data;
        }
    }
}
